package com.daqian.chance.example.admin.core.filter;

import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.subject.Subject;
import org.apache.shiro.web.filter.authz.AuthorizationFilter;
import org.springframework.stereotype.Component;

import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;

/**
 * 权限认证过滤器
 * @author: daqian
 * @date: 2019/9/30 15:21
 */
@Component
@Slf4j
public class ShiroAuthorizationFilter extends AuthorizationFilter {
    @Override
    protected boolean isAccessAllowed(ServletRequest servletRequest, ServletResponse servletResponse, Object mappedValue) throws Exception {
        //todo xxx 待完善
        Subject subject = this.getSubject(servletRequest, servletResponse);
        log.debug("1：{}", subject.isAuthenticated());
        log.debug("2：{}", subject.getSession());
        log.debug("3：{}", subject.getPreviousPrincipals());
        log.debug("4：{}", subject.getPrincipal());
        log.debug("5：{}", subject.isPermitted("/sysUser"));
        log.debug("6：{}", subject.isPermitted("/sysMenu"));

        log.debug("7：{}", mappedValue);

        /**获取用户信息，可以根据用户信息查看有没有对应模块的权限*/
        String[] roles = (String[]) mappedValue;
        if (roles == null || roles.length == 0) {
            return true;
        }
        for (String role : roles) {
            if (subject.hasRole(role)) {
                return true;
            }
        }
        return false;
    }
}
